package com.gljr.exam.tools;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;

import com.gljr.exam.dao.UserRepository;
import com.gljr.exam.domain.User;

public class ExamUniTools {
	@Autowired
	private static UserRepository userRepository;
	// @Autowired
	// private static ExamPaperQuestionRepository ExamPaperQuestionRepository;

	// static Long maxidlong = ExamPaperQuestionRepository.findMaxID();
	// int maxnum = new Long(maxidlong).intValue();
	Random random = new Random();

	long[] ranNumber(int maxnum) {
		long b[] = new long[maxnum];
		int n = 0;
		while (n < maxnum) {
			b[n] = random.nextInt(maxnum) + 1;

			if (n > 0) {
				int i = 0;
				while (i < n) {
					if (b[i] == b[n]) {
						break;
					}
					i++;
				}
				if (i == n) {
					n++;
				}
			} else {
				n++;
			}
		}
		return b;
	}

	public static long[] genArr(int maxnum) {
		ExamUniTools randomNumber = new ExamUniTools();
		long[] c = randomNumber.ranNumber(maxnum);
		for (long i : c) {
			System.out.println(i);
		}
		return c;

	};

	public static long getRandom(int i) {
		ExamUniTools randomNumber = new ExamUniTools();
		long[] c = randomNumber.ranNumber(i);

		return c[i];// 取出数组中的某下标的一个值
	}

	public static synchronized String generateTimeSuffix() {
		SimpleDateFormat format = new SimpleDateFormat("yyMMddHHmmssSSSS");// yyyyMMddHHmmss四位毫秒
		String suffix = format.format(new Date());
		return suffix;
	}

	public static String getSessionUsername(Model model) {
		User userGETObj = (User) model.asMap().get("user");
		String userGET = userGETObj.getUsername();
		System.out.println("打印出uploadexam的用户名---->" + userGET);
		return userGET;
	}

	public static String getSessionRealname(Model model) {
		User userGETObj = (User) model.asMap().get("user");
		String userGET = userGETObj.getRealname();
		System.out.println("打印出uploadexam的用户名---->" + userGET);
		return userGET;
	}

	public static String isAdmin(Model model) {
		if (model.asMap().get("user") == null) {
			model.addAttribute("err", "您未登录之前不可以验证题目题,请先登录!");
			return "redirect:/errpage";
		}
		String currentUsername = ExamUniTools.getSessionUsername(model);
		boolean adminflag = userRepository.getIfAdmin(currentUsername);
		if (adminflag) {
			System.out.println("打印出是否为admin---->" + adminflag);
		}
		return "redirect:/errpage";

	}

	public static String generateGetKey() {
		for (int i = 1; i <= 3; i++) {
			String key = "key" + i;
			System.out.println(key);

		}
		return "";

	}

	public static String generateSixRandom() {
		Long long1 = (long) ((Math.random() * 9 + 1) * 100000);
		String loString = Long.toString(long1);
		return loString;
	}

	// public static void main(String[] args) {
	// System.out.println("数组对象---->" + ExamUniTools.genArr(8));//
	// 动态根据数字生成不同的随机数
	// System.out.println(ExamUniTools.getRandom(0));
	// System.out.println(ExamUniTools.getRandom(1));
	// System.out.println(ExamPaperQuestionRepository.findMaxID());
	// System.out.println(ExamUniTools.generateTimeSuffix());
	// System.out.println(ExamUniTools.generateGetKey());
	// System.out.println(maxidint);
	// }
}